package cn.zn.modules.appoin.controller;

import cn.zn.framework.common.abs.AbstractController;
import cn.zn.framework.common.utils.Constant;
import cn.zn.framework.common.utils.R;
import cn.zn.modules.appoin.entity.AppoinTimeMain;
import cn.zn.modules.appoin.entity.dto.AppoinTimeMainSaveDTO;
import cn.zn.modules.appoin.service.AppoinTimeDetailService;
import cn.zn.modules.appoin.service.AppoinTimeMainService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;

/**
 * 生成：工作日期
 *
 * @author zn
 * @email zhunahao@qq.com
 * @date 2020-09-01 14:15:28
 */
@RestController
@RequestMapping("appoin/appointimemain")
public class AppoinTimeMainController extends AbstractController {
    @Autowired
    AppoinTimeMainService appoinTimeMainService;
    @Autowired
    AppoinTimeDetailService appoinTimeDetailService;

    /**
     * 工作人员列表（时间段）
     *
     * @param params
     * @return
     */
    @PostMapping("workIntervalUserList")
    public R workIntervalUserList(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("startDate")) {
            return R.error("startDate 不能为空");
        }
        if (!params.containsKey("endDate")) {
            return R.error("endDate 不能为空");
        }
        return R.ok().put("list", appoinTimeMainService.queryByInterval(params.get("startDate").toString(), params.get("endDate").toString()));
    }

    /**
     * 工作人员列表（一天）
     *
     * @param params
     * @return
     */
    @PostMapping("workUserList")
    public R workUserList(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("appoinDate")) {
            return R.error("appoinDate 不能为空");
        }
        return R.ok().put("list", appoinTimeMainService.queryByDate(params.get("appoinDate").toString(), null));
    }

    /**
     * 无工作人员列表（一天）
     *
     * @param params
     * @return
     */
    @PostMapping("noWorkUserList")
    public R noWorkUserList(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("appoinDate")) {
            return R.error("appoinDate 不能为空");
        }
        return R.ok().put("list", appoinTimeMainService.queryNoWorkByDate(params.get("appoinDate").toString(), params));
    }

    /**
     * 保存工作人员
     *
     * @param appoinTimeMainSaveDTO
     * @return
     */
    @PostMapping("saveWorkUser")
    public R saveWorkUser(@RequestHeader(required = false) Long tenantId, @Valid @RequestBody AppoinTimeMainSaveDTO appoinTimeMainSaveDTO) {
        appoinTimeMainService.saveWorkUser(tenantId, appoinTimeMainSaveDTO);
        return R.ok();
    }

    /**
     * 暂停工作时间
     *
     * @param id
     * @return
     */
    @PostMapping("saveWorkState/pause/{id}")
    public R saveWorkStatePause(@PathVariable String id) {
        AppoinTimeMain appoinTimeMain = new AppoinTimeMain();
        appoinTimeMain.setId(id);
        appoinTimeMain.setIsActive(false);
        appoinTimeMainService.updateById(appoinTimeMain);
        return R.ok();
    }

    /**
     * 启用工作时间
     *
     * @param id
     * @return
     */
    @PostMapping("saveWorkState/play/{id}")
    public R saveWorkStatePlay(@PathVariable String id) {
        AppoinTimeMain appoinTimeMain = new AppoinTimeMain();
        appoinTimeMain.setId(id);
        appoinTimeMain.setIsActive(true);
        appoinTimeMainService.updateById(appoinTimeMain);
        return R.ok();
    }

    /**
     * 工作时间详情
     *
     * @param mainId
     * @return
     */
    @PostMapping("detail/{mainId}")
    public R detail(@PathVariable String mainId, @RequestBody Map<String, Object> params) {
        return R.ok().put("page", appoinTimeDetailService.queryByMainId(mainId, params));
    }

    /**
     * 待生成时间详情
     *
     * @param mainId
     * @return
     */
    @GetMapping("detail/save/{mainId}")
    public R detailSaveInfo(@PathVariable String mainId) {
        return R.ok().put("appoinwork", appoinTimeDetailService.queryAppoinWork(mainId));
    }

    /**
     * 保存时间详情
     *
     * @param mainId
     * @return
     */
    @PostMapping("detail/save/{mainId}")
    public R detailSave(@PathVariable String mainId) {
        appoinTimeDetailService.saveGenerateDetails(mainId);
        return R.ok();
    }

    /**
     * 删除明细
     *
     * @param ids
     * @return
     */
    @PostMapping("detail/delete")
    public R detailDelete(@RequestBody String[] ids) {
        appoinTimeDetailService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 我的工作安排（时间段）
     *
     * @return
     */
    @PostMapping("my/workInterval")
    public R myWorkInterval(@RequestBody Map<String, Object> params) {
        if (this.loginUser().getUserId() == Constant.SUPER_ADMIN) {
            return R.ok().put("list", Lists.newArrayList());
        }
        if (!params.containsKey("startDate")) {
            return R.error("startDate 不能为空");
        }
        if (!params.containsKey("endDate")) {
            return R.error("endDate 不能为空");
        }
        return R.ok().put("list", appoinTimeMainService.queryByIntervalAndUser(this.loginUser().getUserCode(), params.get("startDate").toString(), params.get("endDate").toString()));
    }
}
